﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPScroller.SPScrollerWebPart
{
    [ToolboxItemAttribute(false)]
    public class SPScrollerWebPart : WebPart
    {
        private string _itemsList;

        [WebBrowsable(true), WebDisplayName("Associated List"),
WebDescription("The list containing the items you want to display."),
      Personalizable(PersonalizationScope.Shared), Category("SPScroller Configuration")]
        public string ItemsList
        {
            get { return _itemsList; }
            set { _itemsList = value; }
        }

        private string _headerField;
        [WebBrowsable(true), WebDisplayName("Header Field"),
WebDescription("The header field."),
      Personalizable(PersonalizationScope.Shared), Category("SPScroller Configuration")]
        public string HeaderField
        {
            get { return _headerField; }
            set { _headerField = value; }
        }

        private string _bodyText;
        [WebBrowsable(true), WebDisplayName("Body Field"),
WebDescription("The body field."),
      Personalizable(PersonalizationScope.Shared), Category("SPScroller Configuration")]
        public string BodyText
        {
            get { return _bodyText; }
            set { _bodyText = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            //Register our jquery scripts

            //jquery minified
            ScriptLink.Register(this.Page, "SPScroller/Scripts/jquery.min.js", false);

            //scrollto jquery library (required by serialscroll)
            ScriptLink.Register(this.Page, "SPScroller/Scripts/jquery.scrollTo-1.4.2-min.js", false);

            //serialscroll library
            ScriptLink.Register(this.Page, "SPScroller/Scripts/jquery.serialScroll-1.2.2-min.js", false);

            //initiation script controls speed, transitions of web part
            ScriptLink.Register(this.Page, "SPScroller/Scripts/init.js", false);

            //register css stylesheet in the style library of the site - user can modify this easily
            CssRegistration.Register("/Style Library/spscroller.css");
        }


        protected override void CreateChildControls()
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = null;
            try
            {
                //Get the list specifi ed in the tool pane
                list = web.Lists[ItemsList];

                //CAML query gets all non null items in the list
                string camlQueryText = string.Format("<Query><Where><IsNotNull><FieldRef Name=\"Title\" /></IsNotNull></Where></Query>");
                SPQuery camlQuery = new SPQuery();
                camlQuery.Query = camlQueryText;
                SPListItemCollection items = list.GetItems(camlQuery);

                //output the list items with an html literal
                if (items.Count != 0)
                {
                    Literal htmlLiteral = new Literal();

                    htmlLiteral.Text += "<div id=\"screen\"><div style=\"width: 120px; height: 249px; float:left\" id=\"tom\">" +
                        "<ul>";

                    //navigation
                    for (int i = 0; i < items.Count; i++)
                    {
                        SPListItem item = items[i];
                        htmlLiteral.Text += "<li>" +
                            "<a href=\"#\">" + item["Title"] + "</a>" +
                            "</li>";
                    }

                    //panels
                    htmlLiteral.Text += "<div id=\"sections\"><ul>";

                    for (int i = 0; i < items.Count; i++)
                    {
                        SPListItem item = items[i];
                        htmlLiteral.Text += "<li>" +
                            "<div><h2>" + item[HeaderField] + "</h2>" +
                            "<p>" + item[BodyText] + "</p>" +
                            "</div>" +
                            "</li>";
                    }

                    htmlLiteral.Text += "</div></div>";


                    this.Controls.Add(htmlLiteral);
                }

                else
                {
                    Literal errorMessage = new Literal();
                    errorMessage.Text = "No items returned.";
                    this.Controls.Add(errorMessage);
                }
            }
            catch
            {
                Literal errorMessage = new Literal();
                errorMessage.Text =
                String.Format("A list with name {0} does not exist in this site.",
                ItemsList);
                this.Controls.Add(errorMessage);
            }
        }
    }
}
